# -*- coding: utf-8 -*-
from django.contrib import messages
from django.shortcuts import redirect
from django.utils.translation import ugettext_lazy as _
from django.views.generic import FormView, TemplateView
from django.views.generic.detail import DetailView
from django.views.generic.list import ListView

from userprofiles.mixins import LoginRequiredMixin
from .forms import ProfileForm 

from django.contrib.auth import get_user_model


class ProfileDetailView(LoginRequiredMixin, DetailView):
    template_name = 'accounts/profile.html'
    model = get_user_model()
    context_object_name = 'user_list'
    slug_field = "username"
    slug_url_kwarg = 'name'
    pk_url_kwarg = 'id'

userprofile = ProfileDetailView.as_view()


class ProfileListView(LoginRequiredMixin, ListView):
    template_name = 'accounts/list.html'
    model = get_user_model()
    paginate_by = 20
    # need pagination

listprofile = ProfileListView.as_view()


class MyProfileView(LoginRequiredMixin, TemplateView):
    template_name = 'userprofiles/profile.html'

    def get_context_data(self, **kwargs):
        return {
            'user': self.request.user,
        }

myprofile = MyProfileView.as_view()

class ProfileChangeView(LoginRequiredMixin, FormView):
    form_class = ProfileForm
    template_name = 'accounts/profile_change.html'

    def get_form_kwargs(self):
        print 'get_form_kwargs'
        kwargs = super(ProfileChangeView, self).get_form_kwargs()
        print kwargs
        print get_profile_model().objects.all()
        print self.request.user
        kwargs['instance'] = get_profile_model().objects.get(
            user=self.request.user)
        print kwargs

        if up_settings.REGISTRATION_FULLNAME:
            kwargs['initial'].update({
                'first_name': self.request.user.first_name,
                'last_name': self.request.user.last_name,
                'email': self.request.user.email
            })
            print kwargs
        return kwargs

    def form_valid(self, form):
        form.save()
        messages.success(self.request, _(u'Profile changed'))
        return redirect(up_settings.PROFILE_CHANGE_DONE_URL)

myprofile_change = ProfileChangeView.as_view()
